//
//  TypeColorViewController.m
//  Manage
//
//  Created by 杨康 on 2017/11/15.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "TypeColorViewController.h"

@interface TypeColorViewController ()
{
    NSArray *_colorArr;
    NSArray *_modelArr;
    UISegmentedControl *_TypeSegmentedControl ;
}
@property(nonatomic,assign)NSInteger acType;
@property(nonatomic,strong)ykTableView *tableView;
@end

@implementation TypeColorViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getTypeColorInfo];
}
-(void)getTypeColorInfo
{
    NSString *URLString = @"/axq/admin/brand/get";
    
    [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:nil RequestSuccess:^(NSURLResponse *response, id responseObject) {
        NSLog(@"%@",responseObject);
        NSString *code = responseObject[@"code"];
        if([code isEqualToString:@"10008"]){
            if (responseObject[@"data"]) {
                NSDictionary *data=responseObject[@"data"];
//                NSString *colorStr=[NSString stringWithFormat:@"%@",data[@"colors"]];
//                _colorArr=[colorStr componentsSeparatedByString:@","];
//                NSString *modelStr=[NSString stringWithFormat:@"%@",data[@"models"]];
//                _modelArr=[modelStr componentsSeparatedByString:@","];
                _colorArr=data[@"colors"];
                _modelArr=data[@"models"];
                [self TypeAction:_TypeSegmentedControl];
            }
            
        }
        else
        {
            NSString *str=responseObject[@"msg"];
            
            [self.view makeToast:str];
        }
    } RequestFailed:^(NSError *error) {
        
    }];

}
-(void)saveAction:(NSString *)saveStr
{
    NSString *URLString;
    NSDictionary *parameters;
    if (self.acType==0) {
        URLString = @"/axq/admin/brand/models/save";
        parameters = @{@"models": saveStr};
    }else if (self.acType==1)
    {
        URLString = @"/axq/admin/brand/colors/save";
        parameters = @{@"colors": saveStr};
    }
    
    
    
    
    [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
        NSLog(@"%@",responseObject);
        NSString *code = responseObject[@"code"];
        if([code isEqualToString:@"50001"]){
            [self getTypeColorInfo];
        }
        else
        {
            NSString *str=responseObject[@"msg"];
            
            [self.view makeToast:str];
        }
    } RequestFailed:^(NSError *error) {
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor=[UIColor colorF];
    self.navigationController.navigationBar.barTintColor = [UIColor colorQ];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    self.navigationItem.title = @"型号颜色";
//    UIButton *leftButton=(UIButton *)self.navigationItem.leftBarButtonItem.customView;
//    leftButton.selected=YES;
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
//                                                                                           target:self
//                                                                                           action:@selector(addAction)];
//    self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    
    

    
    self.tableView=[SingLeton myTableViewWithFrame:CGRectMake(0, TOP_HEIGHT+50, SCREEN_WIDTH, SCREEN_HEIGHT-TOP_HEIGHT-50-44-BOTTOM_HEIGHT) withRefresh:NO didSelect:^(ykTableView *tableView, NSIndexPath *indexPath) {
        
    }];
    [self.view addSubview:self.tableView];
    
    
    UISegmentedControl *TypeSegmentedControl = [[UISegmentedControl alloc] initWithItems:
                                                [NSArray arrayWithObjects:
                                                 @"电动车型号",
                                                 
                                                 @"电动车颜色",
                                                 nil]];
    _TypeSegmentedControl=TypeSegmentedControl;
    TypeSegmentedControl.apportionsSegmentWidthsByContent = YES;
    TypeSegmentedControl.layer.masksToBounds=YES;
    TypeSegmentedControl.layer.cornerRadius=5;
    TypeSegmentedControl.tintColor = [UIColor colorF];
    NSDictionary *dics = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor color6],NSForegroundColorAttributeName,[UIFont systemFontOfSize:12.0],NSFontAttributeName,nil];
    [TypeSegmentedControl setTitleTextAttributes:dics forState:UIControlStateNormal];
    [TypeSegmentedControl setBackgroundImage:[NetworkPath createImageWithColor:[UIColor colorQ]]
                                    forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [TypeSegmentedControl setBackgroundImage:[NetworkPath createImageWithColor:[UIColor whiteColor]]
                                    forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    [TypeSegmentedControl addTarget:self action:@selector(TypeAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:TypeSegmentedControl];
    [TypeSegmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(TOP_HEIGHT+10);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-20, 30));
        
    }];
    
    
    TypeSegmentedControl.selectedSegmentIndex=0;
    [self TypeAction:TypeSegmentedControl];
    
    
    
    
    
    
    
    
    
    
    YKButton *saveButton=[SingLeton endButton:[UIColor colorQ] andTitle:@"保存" RequestSuccess:^(UIButton *btn) {
        NSString *saveStr;
        NSArray *dataArr=self.tableView.dataArray[0];
        for (int i=0; i<dataArr.count; i++) {
            
            cellData *dataObj=dataArr[i];
            if (i==0) {
                saveStr=dataObj.text;
            }else
            {
                saveStr=[saveStr stringByAppendingString:[NSString stringWithFormat:@",%@",dataObj.text]];
            }
        }
        NSLog(@"%@",saveStr);
        [self saveAction:saveStr];
        
    }];
    [self.view addSubview:saveButton];
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 44));
    }];
    saveButton.layer.cornerRadius=0;
    
    
    
}
- (void)TypeAction:(UISegmentedControl *)segmentedControl
{
    self.acType = segmentedControl.selectedSegmentIndex;
    switch (self.acType) {
        case 0:
        {
            self.tableView.className=@"Type3";
            NSMutableArray *itemArr=[NSMutableArray array];
            
            
            
            if (_modelArr) {
                if (_modelArr.count) {
                    for (int i=0; i<20; i++) {
                        
                        if (i<_modelArr.count) {
                            cellData *dataObj=[[cellData alloc]init];
                            dataObj.text=_modelArr[i];
                            
                            [itemArr addObject:dataObj];
                        }else
                        {
                            cellData *dataObj=[[cellData alloc]init];
                            dataObj.text=@"";
                            
                            [itemArr addObject:dataObj];
                        }
                        
                    }
                }else
                {
                    for (int i=0; i<20; i++) {
                        cellData *dataObj=[[cellData alloc]init];
                        dataObj.text=@"";
                        
                        [itemArr addObject:dataObj];
                    }
                }
            }else
            {
                for (int i=0; i<20; i++) {
                    cellData *dataObj=[[cellData alloc]init];
                    dataObj.text=@"";
                    
                    [itemArr addObject:dataObj];
                }
            }
            
            
            
            
            
            [self.tableView.dataArray addObject:itemArr];
            [self.tableView reloadData];
        }
            break;
        case 1:
        {
            self.tableView.className=@"Type4";
            NSMutableArray *itemArr=[NSMutableArray array];
            

            if (_colorArr) {
                if (_colorArr.count) {
                    for (int i=0; i<20; i++) {
                        
                        if (i<_colorArr.count) {
                            cellData *dataObj=[[cellData alloc]init];
                            dataObj.text=_colorArr[i];
                            
                            [itemArr addObject:dataObj];
                        }else
                        {
                            cellData *dataObj=[[cellData alloc]init];
                            dataObj.text=@"";
                            
                            [itemArr addObject:dataObj];
                        }
                        
                    }
                }else
                {
                    for (int i=0; i<20; i++) {
                        cellData *dataObj=[[cellData alloc]init];
                        dataObj.text=@"";
                        
                        [itemArr addObject:dataObj];
                    }
                }
            }else
            {
                for (int i=0; i<20; i++) {
                    cellData *dataObj=[[cellData alloc]init];
                    dataObj.text=@"";
                    
                    [itemArr addObject:dataObj];
                }
            }
            
            
            
            
            [self.tableView.dataArray addObject:itemArr];
            [self.tableView reloadData];
            
        }
            break;
        
            
            
        default:
            break;
    }
}
-(void)addAction
{
    NSMutableArray *itemArr=[NSMutableArray array];
    
    
    for (int i=0; i<10; i++) {
        cellData *dataObj=[[cellData alloc]init];
        dataObj.text=@"";
        
        [itemArr addObject:dataObj];
    }
    
    [self.tableView.dataArray addObject:itemArr];
    [self.tableView reloadData];
}

@end
